<?php

namespace app\admin\controller;

use app\admin\builder\Form;
use app\admin\builder\Table;

/**
 * 模块管理
 * Class Module
 * @package app\admin\controller
 */
class Module extends Admin
{


    public function initialize()
    {

        parent::initialize();
    }


    public function _leftSlide()
    {
        $slide = [
            [
                'title' => '模块列表',
                'child' => [
                    ['menu' => 'admin/module/modules', 'title' => '模块列表', 'slide_name' => 'modules'],
                ]
            ],
            /*            [
                            'title' => '模块配置',
                            'child' => [
                                ['menu' => 'admin/module/config', 'title' => '模块配置', 'slide_name' => 'config'],
                            ]
                        ],*/
        ];

        return $slide;
    }


    public function modules()
    {
        $table = new Table();
        if (Request()->isPost()) {


        } else {
            $r = 30;
            $list = scandir($this->appPath);
            $data = [];
            $modulelist = db('module')->column('name,title,status', 'name');
            $exModule = [
                'admin', 'common', 'core', 'user'
            ];
            foreach ($list as $datum) {
                $config = [];
                if ($datum != '.' && $datum != '..') {
                    if (is_dir($this->appPath . '/' . $datum) && !in_array($datum, $exModule)) {
                        $configPath = $this->appPath . '/' . $datum . '/config.php';
                        if (is_file($configPath)) {
                            $config = require_once($configPath);
                            $config['status'] = '未安装   <a href="javascript:" class="layui-btn layui-btn-xs layui-btn-normal"
                                               data-url="' . url('installmodule') . '" data-role="ajax-post">安装</a>';
                            if (isset($modulelist[$config['name']]) && $modulelist[$config['name']]['status'] == 1) {
                                $config['status'] = '已安装 <a href="javascript:" class="layui-btn layui-btn-xs layui-btn-warm"
                                               data-url="' . url('uninstallmodule') . '" data-role="ajax-post">卸载</a>';
                            }
                            $config['id'] = $config['name'];
                            $data[] = $config;
                        }
                    }
                }
            }
            // dump($data);
            $this->_setSlide('modules');
            return $table->setTitle('模块列表')
                ->dataSource(['data' => $data], 'user')
                ->button('获取选中项', ['data-role' => 'get_checked'])
                // ->field('id', 'ID')
                ->field('name', '名称')
                ->field('title', '标题')
                ->field('description', '描述')
                ->field('author', '作者')
                ->field('version', '版本')
                ->fieldHtml('status', '状态')
                ->action('配置', ['url' => url('forum/admin/config'), 'class' => 'layui-btn-primary'])
                ->show();
        }

    }


    public function uninstallModule()
    {
        $moduleName = input('id', '', 'text');
        $name1 = parse_name($moduleName, 1);
        $time = time();
        $module = db('module')->where('name', $moduleName)->find();
        if (empty($module)) {
            $this->error('卸载失败，未找到模块');
        }
        $data = [
            'status' => 0,
            'update_time' => $time,
        ];
        db('module')->where('name', $moduleName)->update($data);
        $class_name = "\application\\$name1\controller\Admin";
        if (class_exists($class_name) && method_exists(controller($class_name), 'uninstall')) {
            $rs = controller($class_name)->uninstall();
        }


        cache('all_modules', null);
        $this->success('卸载成功');
    }

    public function installModule()
    {
        $moduleName = input('id', '', 'text');
        $name1 = parse_name($moduleName, 1);
        $info = require_once($this->appPath . '/' . $name1 . '/config.php');

        $time = time();
        $module = db('module')->where('name', $moduleName)->find();
        $data = [
            'name' => $info['name'],
            'title' => $info['title'],
            'description' => $info['description'],
            'author' => $info['author'],
            'version' => $info['version'],
            'status' => 1,
            'config' => '',
            'update_time' => $time,
        ];
        if ($module) {

            db('module')->where('name', $moduleName)->update($data);
        } else {
            $data['create_time'] = $time;
            db('module')->insert($data);
        }

        $class_name = "\application\\$name1\controller\Admin";
        if (class_exists($class_name) && method_exists(controller($class_name), 'install')) {
            $rs = controller($class_name)->install();
        }

        cache('all_modules', null);
        $this->success('安装成功');
    }


}
